package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* SettingView
* */
class SettingView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_FF80AB)
        }
    }

    private val mPath by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy)

            val mStrokeWidth = (cx / 20).coerceAtLeast(minStrokeWidth)
            mPaint.strokeWidth = mStrokeWidth

            val offsetAngle0 = 18.0
            val offsetAngle1 = 12.0

            val baseValue10 =  cx * 8 / 11
            val baseValue20 =  cx * 10 / 11
            val baseValue30 =  cx * 6 / 11

            drawCircle(0F, 0F, cx * 5 / 14, mPaint)

            for (i in 1..pieces) {
                //78  90  102  60 -- 120
                //138 150 162  120 -- 180
                //198 210 222  180 -- 240
                //258 270 282  240 -- 300
                //318 330 342  300 -- 360
                //18  30  42    0  -- 60
                mPath.apply {
                    reset()

                    val radius1 = Math.toRadians(baseAngle.toDouble() * i - offsetAngle0)
                    val conValue10 = Math.cos(radius1).toFloat()
                    val sinValue10 = Math.sin(radius1).toFloat()
                    val con10 = baseValue10 * conValue10
                    val sin10 = baseValue10 * sinValue10
                    moveTo(con10, sin10)

                    val radius2 = Math.toRadians(baseAngle.toDouble() * i - offsetAngle1)
                    val conValue20 = Math.cos(radius2).toFloat()
                    val sinValue20 = Math.sin(radius2).toFloat()
                    val con20 = baseValue20 * conValue20
                    val sin20 = baseValue20 * sinValue20
                    lineTo(con20, sin20)

                    val radius3 = Math.toRadians(baseAngle.toDouble() * i + offsetAngle1)
                    val conValue30 = Math.cos(radius3).toFloat()
                    val sinValue30 = Math.sin(radius3).toFloat()
                    val con30 = baseValue20 * conValue30
                    val sin30 = baseValue20 * sinValue30
                    lineTo(con30, sin30)

                    val radius4 = Math.toRadians(baseAngle.toDouble() * i + offsetAngle0)
                    val conValue40 = Math.cos(radius4).toFloat()
                    val sinValue40 = Math.sin(radius4).toFloat()
                    val con40 = baseValue10 * conValue40
                    val sin40 = baseValue10 * sinValue40
                    lineTo(con40, sin40)

                    val radius5 = Math.toRadians(baseAngle.toDouble() * i + 30.0)
                    val conValue50 = Math.cos(radius5).toFloat()
                    val sinValue50 = Math.sin(radius5).toFloat()
                    val con50 = baseValue30 * conValue50
                    val sin50 = baseValue30 * sinValue50

                    val radius6 = Math.toRadians(SettingView2.baseAngle.toDouble() * (i + 1) - offsetAngle0)
                    val conValue60 = Math.cos(radius6).toFloat()
                    val sinValue60 = Math.sin(radius6).toFloat()
                    val con60 = baseValue10 * conValue60
                    val sin60 = baseValue10 * sinValue60

                    quadTo(con50, sin50, con60, sin60)
                }

                drawPath(mPath, mPaint)
            }

            restore()
        }
    }

    companion object {
        const val baseAngle = 60
        const val pieces = 6
    }
}